//
//  UserViewController.m
//  A_Comic
//
//  Created by sos on 16/1/23.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "UserViewController.h"
#import "EnshrineViewController.h"
#import "MyDownLoadViewController.h"
#import "ClearData.h"
@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@end

@implementation UserViewController
- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    // Do any additional setup after loading the view.
    
    //创建tableview
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height+20) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.frame.size.height/2-20)];
    headView.backgroundColor = [UIColor orangeColor];

    self.tableView.tableHeaderView = headView;
    [self.tableView release];
    
    
    //登录头像
    UIImageView *qqImage = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 130, 130)];
    qqImage.center = headView.center;
    qqImage.layer.cornerRadius = 130/2;
    qqImage.clipsToBounds = YES;
    qqImage.backgroundColor = [UIColor redColor];
    [headView addSubview:qqImage];
    [qqImage release];
    
    //登录标签
    UILabel *dengLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    dengLabel.center = CGPointMake(headView.center.x, qqImage.center.y+qqImage.frame.size.height/2+30);
    dengLabel.text = @"登录";
    dengLabel.textAlignment = NSTextAlignmentCenter;
    dengLabel.textColor = [UIColor blackColor];
//    dengLabel.backgroundColor = [UIColor purpleColor];
    
    [headView addSubview:dengLabel];
    [headView release];
    
    
    
    
}
//协议方法

//区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

//cell高
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 2;
    }else{
        return 1;
    }
    
}
//赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.textLabel.text = @"我的收藏";
    }else if (indexPath.section == 0 && indexPath.row == 1){
        cell.textLabel.text = @"我的下载";
    }else if (indexPath.section == 1 && indexPath.row == 0){
        cell.textLabel.text = @" 清除缓存";
        UILabel *clearLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3*2, 0, 100, cell.frame.size.height)];
        clearLabel.tag = 8989;

//        clearLabel.center = CGPointMake(cell.textLabel.center.x+100, cell.textLabel.center.y);
//        clearLabel.backgroundColor = [UIColor redColor];
        [cell addSubview:clearLabel];
        clearLabel.textAlignment = NSTextAlignmentRight;
        
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
//        NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        CGFloat h;

//        if (arr != nil) {

            h = [ClearData folderSizeAtPath:path];
//        }else{
//            h = 0.00;

//        }
        
//        CGFloat h = [ClearData fileSizeAtPath:path];
//        if (h != h) {
//            h = [ClearData fileSizeAtPath:path];
//        }
        
            clearLabel.text = [NSString stringWithFormat:@"%.2fMB",h];

         
        
        
        
        NSLog(@"%@",NSHomeDirectory());
        
    }else if (indexPath.section == 1 && indexPath.row == 1){
        cell.textLabel.text = @"关于我们";
    }else if (indexPath.section == 2){
        cell.textLabel.text = @"设置";
    }
    

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}


//区与区间的间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
//    view.backgroundColor = [UIColor whiteColor];
    return [view autorelease];
}

//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        EnshrineViewController *ensh = [[EnshrineViewController alloc]init];
        [self.navigationController pushViewController:ensh animated:YES];
        
        
    }else if (indexPath.section == 0 && indexPath.row == 1){
        static MyDownLoadViewController *myDownLoad;
//        myDownLoad = [[MyDownLoadViewController alloc]init];
        [self.navigationController pushViewController:myDownLoad animated:YES];

    }else if (indexPath.section == 1 && indexPath.row == 0){
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"亲!你确定要抛弃我" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *quxiao = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *queding = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"shoucang.plist"];
//            
//            NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
//            
//            [arr removeAllObjects];
//            [NSKeyedArchiver archivedDataWithRootObject:arr];
//            [NSKeyedArchiver archiveRootObject:arr toFile:path];
            NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
            [ClearData clearCache:path];
            UILabel *label = [self.view viewWithTag:8989];
            [label removeFromSuperview];
            [self.tableView reloadData];
//            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

            NSLog(@"好的");
            
        }];
        [aler addAction:quxiao];
        [aler addAction:queding];
        [self presentViewController:aler animated:YES completion:^{
            
            
        }];

    }else if (indexPath.section == 1 && indexPath.row == 1){

    }else if (indexPath.section == 2){

    }
    
    
}
-(void)viewWillAppear:(BOOL)animated

{
    [super viewWillAppear:animated];
    NSLog(@"123321");
    UILabel *label = [self.view viewWithTag:8989];
//    if (label.text != nil ) {
//        label.text = nil;
//    }
    [label removeFromSuperview];
    [self.tableView reloadData];
    self.navigationController.navigationBarHidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
